import 'dart:convert';
import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/mvp/base_page_presenter.dart';
import 'package:ls_work/net/HttpUtil.dart';
import 'package:ls_work/net/net.dart';
import 'package:ls_work/a_elder/page/elder_content_page.dart';
// import 'package:ls_work/widgets/state_layout.dart';
import 'package:dio/dio.dart';
import 'package:ls_work/net/post_request.dart';

class ElderContentPresenter extends BasePagePresenter<ElderContentPageState> {
  Future fectData(String text, int page, int type,String buildingId,String floorId,String customerNameOrBedNoLike) async {
    Map<String, dynamic> params = Map();
    params["page"] = page;
    params["customerNameOrBedNoLike"] = customerNameOrBedNoLike;
    params["rows"] = 20;
    if (type != null) params["statusSecondId"] = type;
    if (type == 1) {
      params['validSign'] = 1;
    }
    params["isLive"] = '1';
    if(!buildingId.isNullOrEmpty()){
      params["buildingId"] = buildingId;
    }
    if(!floorId.isNullOrEmpty()){
      params["floorId"] = floorId;
    }

    var map = {
      "jsonStr":jsonEncode(params),
      "page":page,
      "rows":20
    };

    view.showProgress();
    HttpUtil.instance
        .getDio()
        .post('${HttpApi.customerElder}',data: map,options: PostRequest.urlencodedHeader)
        .then((r) {
      ///
      view.closeProgress();
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());
        if (map["code"].toString() == "0") {
          if (map['data'] != null) {
            ElderEntity model = ElderEntity.fromJson(map['data']);
            if ((model.rows??[]).length > 0) {
              view.provider.setHasMore((model.rows??[]).length == 20);
              if (page == 1) {
                /// 刷新
                view.provider.list.clear();
                view.provider.addAll(model.rows??[]);
              } else {
                view.provider.addAll(model.rows??[]);
              }
            } else {
              if (page == 1) {
                view.provider.list.clear();
              }
              view.showToast(map["msg"]);
            }
          } else {
            view.showToast(map["msg"]);
          }
        }
      } else {
        view.showToast(r.statusCode.toString());
        view.provider.setHasMore(false);
      }
    }).catchError((e) {
      view.closeProgress();
      print(e.toString());
      view.provider.setHasMore(false);
      view.showToast(getErrorMessage((e as DioError).type));
    });
  }
}
